/**
 * 
 */
package extract.file.processing;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
 * @author chris
 *
 */
public class ReadFile {

    private String directory; //directory containing text files to be read
    private ArrayList<GutFile> fileList = new ArrayList();

    public ReadFile(String directory, ArrayList<GutFile> fileList)
    {
	this.fileList = fileList;
	this.directory = directory;
    }

    //adds all files to arraylist that are greater than 0 bytes
    //uses jsoup to get the title of the book
    //TODO: get the author name
    public void storeFiles() throws IOException
    {

	File dir = new File(directory);
	File[] dirList  = dir.listFiles();
	if(dirList != null)
	{
	    //using a small sample set to avoid memory issues
	    for(int i=0; i<dirList.length; i++)
		//for(int i=0; i<5; i++)
	    {
		if(dirList[i].getName().contains(".txt.utf8.trim") == true)
		{
		    long length = dirList[i].length();
		    if(length > 0)
		    {
			//Commented code will use Jsoup to get the name of the book.
			/*String str = ".";
			int index = dirList[i].getName().indexOf(str);
			String number = dirList[i].getName().substring(0, index);

			Document doc = Jsoup.connect("http://www.gutenberg.org/browse/languages/fr").referrer("http://google.com").userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6").timeout(500*10).get();

			Element title = doc.select("[href*=/ebooks/"+number+"]").first();
			String GUTtitle = title.text();

			Element author = doc.select("[href*=/a#]").first();
			String GUTauthor = author.text();
			GutFile g = new GutFile(GUTauthor,GUTtitle,dirList[i].getName());

			 */
			BufferedReader br = new BufferedReader(new FileReader(dirList[i]));
			String input;
			//	while((input != br.readLine()) == null)
			GutFile g = new GutFile(null,null,dirList[i].getName(),null);
			fileList.add(g);
		    }
		    else
			System.out.println("File is 0 bytes " +dirList[i].getName());
		}
		else
		{
		    System.out.println("Does not contain .txt.utf8.trim");
		}
	    }
	}
	else
	{
	    System.out.println("No files found");
	}

    }

    public void processFile(String dir) throws IOException
    {

	String input;

	int y =0;

	for(GutFile g : fileList)
	{
	    y++;
	    System.out.println(y);
	    BufferedReader reader = new BufferedReader( new FileReader (dir +g.getFileName()));

	    StringBuilder last = new StringBuilder();
	    boolean end = false;
	    while((input = reader.readLine()) != null)
	    {
		if(input.length() > 0)
		{
		    if(end == true)
		    {
			last.append(" ");
			last.append(input);
		    }
		    if(input.contains("End of ") == true)
		    {
			last.append(input);
			end = true;
		    }

		}
	    }

	    StringBuilder title = new StringBuilder();
	    StringBuilder author = new StringBuilder();

	    if(last.toString().contains("EBook"))
	    {
		StringTokenizer strtok = new StringTokenizer(last.toString(), " ");
		while(strtok.hasMoreTokens())
		{

		    if(strtok.nextToken().contains("EBook") == true)
		    {
			strtok.nextToken();
			while(strtok.hasMoreTokens())
			{
			    while(strtok.hasMoreTokens())
			    {
				title.append(strtok.nextToken());
				title.append(" ");
			    }
			}
		    }
		}
		String[] x = title.toString().split("by");
		if(x.length == 2)
		{
		    g.setTitle(x[0].trim());
		    g.setAuthor(x[1].trim());
		}
	    }
	    else
	    {
		StringTokenizer strtok = new StringTokenizer(last.toString(), " ");
		while(strtok.hasMoreTokens())
		{

		    if(strtok.nextToken().contains("Gutenberg's") == true)
		    {
			//strtok.nextToken();
			while(strtok.hasMoreTokens())
			{
			    while(strtok.hasMoreTokens())
			    {
				title.append(strtok.nextToken());
				title.append(" ");
			    }
			}
		    }
		}
		String[] x = title.toString().split(", by");
		if(x.length == 2)
		{
		    g.setTitle(x[0].trim());
		    g.setAuthor(x[1].trim());
		}
	    }

	    reader.close();
	}
    }

    public void formatFile(String dir) throws IOException
    {
	//	boolean directory = new File(dir +"modified/").mkdir();

	//	if(new File(dir +"modified/").exists() == false)
	//	{
	//	    boolean directory = new File(dir +"modified/").mkdir();
	//	}
	//else

	for(GutFile g : fileList)
	{
	    ArrayList<String> sentences = new ArrayList<String>();
	    String input;
	    StringBuilder stringBuilder = new StringBuilder();

	    BufferedReader reader = new BufferedReader( new FileReader (dir +g.getFileName()));

	    File file = new File(dir +"modified/" +g.getFileName());

	    FileWriter fw = new FileWriter(file.getAbsoluteFile());
	    BufferedWriter bw = new BufferedWriter(fw);

	    if (!file.exists()) {
		file.createNewFile();
	    }

	    while((input = reader.readLine()) != null)
	    {
		if(input.length() > 0)
		{
		    stringBuilder.append(input.trim());
		    stringBuilder.append(" ");
		}
	    }

	    StringTokenizer tokenizer = new StringTokenizer(stringBuilder.toString(), ".");
	    while(tokenizer.hasMoreTokens())
	    {
		sentences.add(tokenizer.nextToken());
	    }

	    for(String s : sentences)
	    {
		int words = 0;
		StringTokenizer t = new StringTokenizer(s, " ");
		while(t.hasMoreTokens())
		{
		    words++;
		    t.nextToken();
		}
		bw.write(s +"###" +words);
		bw.write("\n");
		//Sentence sent = new Sentence(s, words);
		//sentenceList.add(sent);
	    }
	    bw.close();
	    reader.close();
	}
    }

    public void printAll()
    {
	for(GutFile f : fileList)
	{
	    System.out.println(f.getFileName() +":::" +f.getSentenceList().size());
	}
    }
}
